package com.yao.longhan.ui.base;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.yao.longhan.BaseApplication;
import com.yao.longhan.eventbus.RxBus;
import com.yao.longhan.eventbus.RxEvent;
import com.yao.longhan.net.BaseCallback;
import com.yao.longhan.net.RetrofitService;
import com.yao.longhan.utils.ToastUtils;

import butterknife.ButterKnife;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.schedulers.Schedulers;

/**
 * Created by heyao on 2017/7/17.
 */

public abstract class BaseFragment extends Fragment implements BaseCallback.CallBackListener {
    protected String TAG = getClass().getSimpleName();
    protected BaseActivity mActivity;
    protected BaseApplication mApp;
    protected RetrofitService mRetrofitService;
    protected Bundle arguments;
    private Disposable mDisposable;

    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
        mActivity = (BaseActivity) context;
        mApp = (BaseApplication) mActivity.getApplication();
        mRetrofitService = mApp.getRetrofitService();
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        arguments = getArguments();
        registerRxBus();
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        initBefore();
        return inflater.inflate(layoutId(), container, false);
    }

    protected void initBefore() {

    }

    protected abstract int layoutId();

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        ButterKnife.bind(this, view);
        initView(savedInstanceState);
        initAfter();
    }

    protected void initView(Bundle savedInstanceState) {

    }

    protected abstract void initAfter();

    @Override
    public void onSuccess(int requestCode, Object object) {

    }

    @Override
    public void onFailed(int requestCode, String message) {
        ToastUtils.showSingleToast(message);
    }

    protected <T> void toActivityForResult(Class<T> clazz, Bundle bundle, int requestCode) {
        Intent intent = new Intent(mActivity, clazz);
        if (bundle != null) {
            intent.putExtras(bundle);
        }
        startActivityForResult(intent, requestCode);
    }

    protected <T> void toActivity(Class<T> clazz, Bundle bundle) {
        Intent intent = new Intent(mActivity, clazz);
        if (bundle != null) {
            intent.putExtras(bundle);
        }
        startActivity(intent);
    }

    private void registerRxBus() {
        mDisposable = RxBus.getDefault().toObservable(RxEvent.class)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(rxEvent -> {
                    if (rxEvent != null) {
                        onEventMainThread(rxEvent);
                    }
                });
    }

    protected void onEventMainThread(RxEvent rxEvent) {

    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        if (!mDisposable.isDisposed()) {
            mDisposable.dispose();
        }
    }
}
